<%
	select case Session("FormName")
		case "F19"
			Session("bChucnang")=5
		case "F20"
			Session("bChucnang")=5
		case "F21"
			Session("bChucnang")=5
	end select 
%>
<!--#include file="..\CheckSession.inc"-->
<!--#include file="..\RootContent.inc"-->
<!--#include file=..\getFormName.inc-->
<%  send= request("Send")
	bNam=Request("bNam")
	bLoaiVT=Request("bLoaiVT")
	bMa=Request("bMa")
	if request("Send") = "ok" then
		bMsg=Request.Form("Message")
		bFromEmail=Request.Form("FromEmail")
		bFromName=Request.Form("FromName")
		bToName=Request.Form("ToName")
		bSubject=Request.Form("Subject")
		bToEmail=Request.Form("ToEmail")
		'Lay IP Mail Server
		sSQL="Select * from Config"
		Set RecIP=server.CreateObject("ADODB.Recordset")
		RecIP.CursorType=3
		RecIP.CacheSize=5
		RecIP.Open sSQL,Conn
		if RecIP.RecordCount then
			IPMailServer=RecIP("IPMailserver")
			Port=RecIP("Port")
		end if						
		Set File = Server.CreateObject("ActiveFile.File")
		set objSendMail = Server.CreateObject("vbSendMail.clsSendMail")
		'objSendMail.ConnectTimeout=5
		objSendMail.SMTPHost =IPMailServer
		objSendMail.SMTPPort = port
		objSendMail.From =Cstr(bFromEmail)       
		objSendMail.FromDisplayName =Cstr(bFromName)   
		'objSendMail.Recipient = Cstr(bToEmail)
		objSendMail.RecipientDisplayName = Cstr(bToName)        
		objSendMail.ReplyToAddress = Cstr(bFromEmail)               
		objSendMail.Subject = Cstr(bSubject)           
		objSendMail.Message = Cstr(bMsg)                  
		objSendMail.AsHTML = True		
		bNam=Request("bNam")
		bLoaiVT=Request("bLoaiVT")
		bMa=Request("bMa")
		Select Case bLoaiVT
			Case 1 'Den
				sSQL="Select * from vanban_noidung where mavanthu='"& bMa &"' and Loaivanthu=1"
				pathAttach=RootContent & "Upload\"& cstr(bNam) &"\Den\"
			Case 2 'Di
				sSQL="Select * from vanban_noidung where mavanthu='"& bMa &"' and Loaivanthu=2"
				pathAttach=RootContent & "Upload\"& cstr(bNam) &"\Di\"
			Case 3 'Duthao
				sSQL="Select * from vanban_noidung where mavanthu='"& bMa &"' and Loaivanthu=3"
				pathAttach=RootContent & "Upload\"& cstr(bNam) &"\Duthao\"
			Case 5 'Quyet dinh
				sSQL="Select * from vanban_noidung where mavanthu='"& bMa &"' and Loaivanthu=5"
				pathAttach=RootContent & "Upload\"& cstr(bNam) &"\Quyetdinh\" 
		End Select
		Set RecA=createObject("adodb.recordset")
		RecA.cursortype=3
		RecA.cachesize=5
		RecA.open sSQL,Conn
		bAttachfile=""
		Att=false
		if RecA.recordcount>=0 then
			Att=true
		end if
		strTmp=""
'Kiem tra xem thuc te tep dinh kem co ton tai khong (duong dan Vly)?
		Do while not RecA.Eof
			File.Name = pathAttach & trim(RecA("Tentep"))
			if File.Exists then
				bAttachfile=bAttachfile+pathAttach+trim(RecA("Tentep"))+";"
			end if
			RecA.Movenext
		Loop
		if Att and  bAttachfile<>"" then
			bAttachfile=left(bAttachfile,len(bAttachfile)-1)
			objSendMail.Attachment=bAttachfile
		end if

		RecA.close
		Set RecA=nothing		

		Dim ctemp,i,j,m,str,lenStr,cc()
		
		str=Trim(Cstr(bToEmail))
		lenStr=len(str)
		
		m=0
		i=1
		For i = 1 To lenStr
			if mid(str,i,1)=";" or mid(str,i,1)="," then
				m=m+1
			end if
		Next
		if mid(str,lenStr,1)<>";" and mid(str,lenStr,1)<>"," then
			m=m+1
		end if
		redim cc(m)
		
		ctemp = ""
		j = 1
		For i = 1 To lenStr
			If Mid(str, i, 1) <> ";" and Mid(str, i, 1) <> "," Then
				ctemp = ctemp + Mid(str, i, 1)
			Else 
				if trim(ctemp)<>"" then
					cc(j) = trim(ctemp)
					ctemp = ""
					j = j + 1
				End If
			End if
		Next		
		if trim(ctemp)<>"" then cc(j) = trim(ctemp)
		
		temp=""
		temp1=""
		for i=1 to m
			objSendMail.Recipient=cc(i)
			if objSendMail.IsValidEmailAddress(objSendMail.Recipient) then
				if objSendMail.Connect() then			
					objSendMail.Send()
					if temp="" then
						temp=cc(i)
					else
						temp=temp+", "+cc(i)
					end if					
					sResults = "<p align=center><FONT FACE = 'times new roman' COLOR = '#800000'>Công văn &#273;&#227; &#273;&#432;&#7907;c g&#7917;i cho " + temp + "</FONT><P>"
					objSendMail.Disconnect()					
				else			
					sResults = "<p align=center><FONT FACE = 'times new roman' COLOR = '#800000'>Kh&#244;ng t&#236;m th&#7845;y IP Mail Server</FONT><P>"
				end if
			else	
				if temp1="" then
					temp1=cc(i)
				else
					temp1=temp1+", "+cc(i)
				end if
					sResults1 = "<p align=center><FONT FACE = 'times new roman' COLOR = '#800000'>&#208;&#7883;a ch&#7881; Email <FONT FACE = 'times new roman' COLOR = red>" + temp1 + " </FONT> kh&#244;ng h&#7907;p l&#7879; </FONT><P>"
			end if
		next		
		Response.Write sResults
		Response.Write sResults1
		
		Set objSendMail = Nothing
	End if
	set TextFile=server.CreateObject("Vinalines.ClsFile")
	bMa=Request("bMa")
	sSQL="Select * from canbo where macanbo ='"& Session("bMaCB") &"'"
	Set RecCB=server.CreateObject("ADODB.Recordset")
	RecCB.CursorType=3
	RecCB.Open sSQL,Conn
	if RecCB.RecordCount then
		bEmail=RecCB("Email")
	end if
	RecCB.Close
	Set RecCB=nothing
	bLoaiVT=Request("bLoaiVT")
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Hệ quản trị văn thư</title>
</head>

<body bgcolor=#F8F9D9 LANGUAGE=javascript onload="return window_onload()"  style="font-family:Times New Roman" topmargin=0 leftmargin=0>
<form name=frm method=post action='Compose.asp?Send=ok&bNam=<%=bnam%>&bma=<%=bma%>&bLoaiVT=<%=bLoaiVT%>'>
<table border="0" width="100%" bgcolor="#bdd5d0" style="border-style: solid" cellpadding=0 cellspacing=1>

<input type=hidden name=SMTPServer>
<input type=hidden name=FromName value='<%=Session("bHoten")%>'>
<input type=hidden name=ToName value="<%=bToName%>">
  <tr>
    <td width="15%" align=center>Gửi từ &nbsp;&nbsp;</td>
	<td width="65%">
    <input type="text" name="FromEmail" value='<%=bEmail%>'  style="font-family:Times New Roman; width:100%" size="20"></td>
    <td width="10%"><Button name="CmdSend"  style="font-family:Times New Roman; width: 100%; height: 25"  LANGUAGE=javascript onclick="return CmdSend_onclick()" accesskey="G"><u>G</u>ửi</Button></td>
	<td width="10%"><Button name="CmdExit"  style="font-family:Times New Roman; width:100%; height: 25"  LANGUAGE=javascript onclick="return CmdExit_onclick()" accesskey="T"><u>T</u>hoát</Button></td>
  </tr>
  <tr>
	<td width="15%" align=center><input type=button name=CmdS value="Người nhận" onclick="javaScript:openW()" style="font-family:Times New Roman; width:100%; background-color=#BDD5D0"></td> 
    <%if Send="ok" then%>
	    <td  width="85%" colspan=3>
        <input type="text" name="ToEmail" value="<%=bToEmail%>" style="font-family:Times New Roman; width:100%" size="20"></td>
	<%else%>
	<td  width="85%" colspan=3>
    <input type="text" name="ToEmail" style="font-family:Times New Roman; width:100%" size="20"></td>
	<%end if%>
  </tr>
  <tr>
    <td width="15%" align=center>Tiêu đề &nbsp;&nbsp;</td>
    <%if Send="ok" then%>
		<td  width="85%" colspan=3>
        <input type="text" name="Subject" value='<%=bSubject%>'  style="font-family:Times New Roman; width:100%" size="20"></td>
	<%else%>
	<td  width="85%" colspan=3> 
	<input type="text" name="Subject" value='<%=request("TxtTrichyeu")%>'  style="font-family:Times New Roman; width:100%" size="20"></td>
	<%end if%>
  </tr>
</table>
<div style='position:relative;top=0px;left=0px;height:80%' width="100%">
		<%				
			
			Select Case bLoaiVT
				Case 1	'Den				  
					path=RootContent & "Upload\"& cstr(bNam) &"\Den\Text\"
					Path=Path & "\" & bMa & ".VT" 
				Case 2 'Di
					path=RootContent & "Upload\"& cstr(bNam) &"\Di\Text\"
					Path=Path & "\" & bMa & ".VT"
				Case 3 'Duthao
					path=RootContent & "Upload\"& cstr(bNam) &"\Duthao\Text\"
					Path=Path & "\" & bMa & ".VT"
				Case 5 'Di
					path=RootContent & "Upload\"& cstr(bNam) &"\Quyetdinh\Text\"
					Path=Path & "\" & bMa & ".VT" 
			End Select
		%>
		<%if request("Send")="ok" then%>
			<textarea name="Message" style='visibility:hidden;position:absolute;top:0px;left:0px' rows="5" cols="20%"><%=bMsg%></textarea>			
		<%else%>
			<textarea name="Message" style='visibility:hidden;position:absolute;top:0px;left:0px' rows="5" cols="20%"><%=TextFile.ReadTextFile(path)%></textarea>			
		<%end if%>
		<div id="plainmsg" style='visibility:hidden;position:absolute;top:0px;left:0px'></div>
			<object id="dynamsg" width="100%" style="position:absolute;top:0px;left:0px" height="100%" data="../editor/deditor.asp" viewastext type="text/x-scriptlet"></object>
		</div>
</form>
<!-- Hien thi d/s tep dinh kem -->
<%
sSQL="Select Tentep,mota from vanban_noidung where Mavanthu like '"& bMa &"' and Loaivanthu like '"& bLoaiVT &"' order by sothutu"
		Set RecT=server.CreateObject("ADODB.Recordset")
		RecT.CursorType=3
		RecT.CacheSize=5
		RecT.Open sSQL,Conn	
		if RecT.RecordCount then
			bAttach="<hr>"
			bAttach=bAttach & "<center><Table Width=95% border=1><TR bgcolor=#000080><TD Width=100% colspan=5 align=center><font face=Arial size=2 color=#FFFFFF><strong><span >Danh s&#225;ch t&#224;i li&#7879;u g&#7855;n k&#232;m</span></strong></font></TD></TR></Table></Center>"
			bAttach=bAttach & "<center><Table><TR>"
			bCol=0
			Do while not RecT.EOF
				File.Name = pathAttach & trim(RecT("Tentep"))
			if File.Exists then
				if bCol=5 then
					bAttach=bAttach & "</TR><TR>"
					bCol=0
				end if
					bAttach=bAttach & "<TD width='20%'><a href=OpenAttach.asp?bNam="& cstr(bnam) &"&bLoaiVT="& bLoaiVT &"&Filename=" & Session("ClsFun").mahoa(lcase(RecT("Tentep"))) & " target=New><center>"
					imgext =lcase(ExtFile(RecT("Tentep")))
					select case imgext
						case "doc"
							bAttach=bAttach & "<IMG SRC="&"..\ImageAttach\doc.gif"&" border=0>"
						case "xls"
							bAttach=bAttach & "<IMG SRC="&"..\ImageAttach\xls.gif"&" border=0>"
						case "gif","jpg","bmp","tif"
							bAttach=bAttach & "<IMG SRC="&"..\ImageAttach\gif.gif"&" border=0>"
						case "txt","log","bat"
							bAttach=bAttach & "<IMG SRC="&"..\ImageAttach\txt.gif"&" border=0>"
						case "htm","html"
							bAttach=bAttach & "<IMG SRC="&"..\ImageAttach\html.gif"&" border=0>"
						case "ppt"
							bAttach=bAttach & "<IMG SRC="&"..\ImageAttach\ppt.gif"&" border=0>"
						case "pdf"
							bAttach=bAttach & "<IMG SRC="&"..\ImageAttach\pdf.gif"&" border=0>"
						case "mdb"
							bAttach=bAttach & "<IMG SRC="&"..\ImageAttach\mdb.gif"&" border=0>"
						case "com","exe"
							bAttach=bAttach & "<IMG SRC="&"..\ImageAttach\exe.gif"&" border=0>"
						case "avi"
							bAttach=bAttach & "<IMG SRC="&"..\ImageAttach\avi.gif"&" border=0>"
						case else
							bAttach=bAttach & "<IMG SRC="&"..\ImageAttach\other.gif"&" border=0>"
					end select
					bAttach=bAttach & "<br><font face=Arial size=2>" & RecT("Mota") & "</font></center></A></TD>"
				if  Session("Attach")="" then
					Session("Attach")="-" & RecT("Tentep")
				else
					Session("Attach")=Session("Attach") & "-" & RecT("Tentep")
				end if
				bCol=bCol+1
			end if
				RecT.MoveNext
			Loop
			Session("Attach")=Session("Attach") & "-" 
			bAttach=bAttach & "</TR></Table></Center>"
		end if
%>
<%=bAttach%>
<!--  -->
</body>
<Script language=vbscript runat=server>
Function ExtFile(filename)
	dim longfilename
	dim VtCham
	longfilename=len(filename)
	VtCham=instr(1,filename,".")
	if VtCham>0 then
		ExtFile=mid(filename,VtCham+1,longfilename-VtCham)
	end if
End Function
</Script>
<SCRIPT ID=clientEventHandlersVB LANGUAGE=VBScript>
	Private Sub poSendMail_SendSuccesful()
		' vbSendMail 'SendSuccesful Event'
		MsgBox "Send Successful!"
		lblProgress = ""
	End Sub
	Private Sub poSendMail_SendFailed(Explanation)

    ' vbSendMail 'SendFailed Event
    MsgBox ("Your attempt to send mail failed for the following reason(s): " & vbCrLf & Explanation)
    lblProgress = ""
    Screen.MousePointer = vbDefault
    cmdSend.Enabled = True
    
End Sub
Private Sub poSendMail_SendSuccesful()

    ' vbSendMail 'SendSuccesful Event'
    MsgBox "Send Successful!"
    lblProgress = ""

End Sub
</script>
<SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
	function window_onload() {
		document.all.dynamsg.html=frm.Message.value;
		frm.ToEmail.focus();
	}
function CmdSend_onclick() {
	frm.Message.value=document.all.dynamsg.html;
	frm.submit();
}

function CmdExit_onclick() {
	window.close();
}

function openW(sObj) {
  popupNguoiNhan = window.open('frmNguoinhan.asp','NguoiNhan','width=300,height=300').focus();
}
</Script>
<%
	Set TextFile=nothing
	Set File = Nothing
%>
</html>